package view.listener;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import control.Storage;
import control.Util;

public class StartButtonListener extends SelectionAdapter {

	private Text text;

	@Override
	public void widgetSelected(SelectionEvent arg0) {

		Control children[] = arg0.display.getActiveShell().getChildren();

		for (int i = 1; i < children.length; i += 2) {

			Combo combo;
			switch (i) {
			case 1:
				text = (Text) children[i];
				if (Util.validateInput(text.getText())) {
					Storage.getInstance().setPlayerName(text.getText());
				} else {
					Storage.getInstance().setPlayerName(Storage.getInstance().getMessages().getString("PLAYER"));
				}

			case 3:
				text = (Text) children[i];
				if (Util.validateInput(text.getText())) {
					Storage.getInstance().setEnemyName(text.getText());
				} else {
					Storage.getInstance().setEnemyName(Storage.getInstance().getMessages().getString("ENEMY"));
				}

				break;

			case 5:
				combo = (Combo) children[i];
				if (Util.validateInput(combo.getText())) {
					String string = combo.getText().substring(0, 1);
					Storage.getInstance().setSize(Integer.valueOf(string));
					Storage.getInstance().setConfigured(true);
					arg0.display.getActiveShell().dispose();
				} else {
					
					MessageBox messageBox = new MessageBox(combo.getShell(), SWT.ICON_WARNING |SWT.OK);
			        
			        messageBox.setText(Storage.getInstance().getMessages().getString("WARNING"));
			        messageBox.setMessage(Storage.getInstance().getMessages().getString("CHOOSE"));
			        messageBox.open();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
					}
					combo.update();
				}

				break;

			}

		}

	}

}
